home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
- Amiga sound driver for Amiga v2600 by Matthew Stroup.
- Allocates channel through OS, then does hardware banging.
- For Amiga v2600 v0.7. April 24, 1997.
- ******************************************************************************/
-
- #include "types.h"
- #include "vmachine.h"
- #include "tiasound.h"
- #include "options.h"
- #include "config.h"
- #include <stdio.h>
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <proto/exec.h>
- #include <hardware/custom.h>
- #include <hardware/dmabits.h>
- #include <devices/audio.h>
-
- /* Could set this up as an option... */
- #define SAMPLE_RATE 8192
-
- #define AUDIO_CLOCK 31400
-
- static int sound_change;
-
- #define SINE 0
- #define FOURBIT 1
- #define FIVEFOURBIT 2
- #define FIVEBIT 3
- #define NINEBIT 4
-
- void stopsound(void);
- void clean_up(void);
-
- struct MsgPort *replymp = NULL;
- struct IOAudio *audio_req = NULL;
-
- UBYTE allocation_array[]={0,1,2,4};
-
- /* The buffer is too large at one second! */
- static unsigned char *sndbuf=0, *bufpt=0;
-
- /* Whether sound is found on start up */
- static int sound_available;
-
- extern struct AudChannel far aud[];
- extern UWORD far dmacon;
-
- /****************************************************************************/
-
- /* Turn on the sound */
- void sound_init (void)
- {
- if(base_opts.sound)
- {
- if ((replymp=(struct MsgPort *)CreatePort(NULL, 0))==0)
- {
- printf("Could not create audio port!\n");
- clean_up();
- return;
- }
-
- if ((audio_req=(struct IOAudio *)CreateExtIO(replymp,sizeof(struct IOAudio)))==0)
- {
- printf("Not enough memory for audio structure!\n");
- clean_up();
- return;
- }
-
- if (OpenDevice(AUDIONAME,0,(struct IORequest *)audio_req,0))
- {
- printf("Could not open the Audio Device!\n");
- audio_req->ioa_Request.io_Device=NULL;
- clean_up();
- return;
- }
-
- audio_req->ioa_Request.io_Command=ADCMD_ALLOCATE;
- audio_req->ioa_Request.io_Message.mn_Node.ln_Pri=127;
- audio_req->ioa_Request.io_Flags = ADIOF_NOWAIT;
- audio_req->ioa_Data=allocation_array;
- audio_req->ioa_Length=sizeof(allocation_array);
- BeginIO((struct IORequest *)audio_req);
-
- if (WaitIO((struct IORequest *)audio_req))
- {
- printf("Sound channel not available!\n");
- clean_up();
- return;
- }
-
- if (audio_req->ioa_Request.io_Unit!=0)
- {
- printf("First sound channel not available!\n");
- clean_up();
- return;
- }
-
- if ((sndbuf=(unsigned char *)AllocMem(AUDIO_CLOCK, MEMF_CHIP|MEMF_CLEAR))==NULL)
- {
- printf("Not enough memory for sound buffer!\n");
- return;
- }
-
- sound_available=1;
-
- if (Verbose) printf("Sound driver initialized\n");
-
- bufpt=sndbuf;
-
- Tia_sound_init(AUDIO_CLOCK, SAMPLE_RATE);
- }
- }
-
- void sound_update(void)
- {
- if(sound_available)
- {
- if(sound_change)
- {
- dmacon=DMAF_AUD0;
- Tia_process(bufpt, SAMPLE_RATE);
- aud[0].ac_ptr=(unsigned short *)sndbuf;
- aud[0].ac_len=SAMPLE_RATE;
- aud[0].ac_per=437;
- aud[0].ac_vol=64;
- dmacon=DMAF_SETCLR|DMAF_AUD0;
- bufpt=sndbuf;
- sound_change=0;
- }
- }
- }
-
- /* Turn off the sound */
- void sound_close (void)
- {
- clean_up();
- if (sound_available)
- {
- dmacon=DMAF_AUD0;
- if (sndbuf) FreeMem(sndbuf, AUDIO_CLOCK);
- }
- }
-
- void sound_freq(int channel, UBYTE freq)
- {
- static BYTE last[2];
- if(sound_available && last[channel]!=freq)
- {
- Update_tia_sound(0x17 + channel, freq);
- sound_change=1;
- last[channel]=freq;
- }
- }
-
- void sound_volume(int channel, UBYTE vol)
- {
- static BYTE last[2];
- if(sound_available && last[channel]!=vol)
- {
- Update_tia_sound(0x19 + channel, vol);
- sound_change=1;
- last[channel]=vol;
- }
- }
-
- void sound_waveform(int channel, UBYTE value)
- {
- static BYTE last[2];
- if(sound_available && last[channel]!=value)
- {
- Update_tia_sound(0x15 + channel, value);
- sound_change=1;
- last[channel]=value;
- }
- }
-
- void clean_up(void)
- {
- if (audio_req && !(audio_req->ioa_Request.io_Error))
- {
- audio_req->ioa_Request.io_Command = ADCMD_FREE;
- DoIO((struct IORequest *)audio_req);
- }
-
- if (audio_req && audio_req->ioa_Request.io_Device)
- CloseDevice((struct IORequest *)audio_req);
-
- if (replymp)
- {
- while (GetMsg(replymp)) sound_change=1;
- DeletePort(replymp);
- }
-
- if(audio_req) DeleteExtIO((struct IORequest *)audio_req);
- }
-